package com.xndc.controller;

import com.xndc.service.iec104.Iec104CommandService;
import com.xndc.service.iec104.Iec104ConnectionManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * IEC 104协议控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/iec104")
public class Iec104Controller {
    
    @Autowired
    private Iec104CommandService iec104CommandService;
    
    @Autowired
    private Iec104ConnectionManager connectionManager;
    
    /**
     * 获取IEC 104连接状态
     */
    @GetMapping("/status")
    public ResponseEntity<Map<String, Object>> getStatus() {
        Map<String, Object> status = new HashMap<>();
        status.put("connectedDevices", connectionManager.getConnectedDeviceCount());
        
        return ResponseEntity.ok(status);
    }
    
    /**
     * 发送单点命令
     */
    @PostMapping("/command/single")
    public ResponseEntity<Map<String, Object>> sendSingleCommand(
            @RequestParam String deviceId,
            @RequestParam int ioa,
            @RequestParam boolean value) {
        
        log.info("接收到单点命令请求: 设备={}, IOA={}, 值={}", deviceId, ioa, value);
        boolean success = iec104CommandService.sendSingleCommand(deviceId, ioa, value);
        
        Map<String, Object> response = new HashMap<>();
        response.put("success", success);
        response.put("message", success ? "命令已发送" : "发送命令失败");
        
        return ResponseEntity.ok(response);
    }
    
    /**
     * 发送双点命令
     */
    @PostMapping("/command/double")
    public ResponseEntity<Map<String, Object>> sendDoubleCommand(
            @RequestParam String deviceId,
            @RequestParam int ioa,
            @RequestParam byte value) {
        
        log.info("接收到双点命令请求: 设备={}, IOA={}, 值={}", deviceId, ioa, value);
        boolean success = iec104CommandService.sendDoubleCommand(deviceId, ioa, value);
        
        Map<String, Object> response = new HashMap<>();
        response.put("success", success);
        response.put("message", success ? "命令已发送" : "发送命令失败");
        
        return ResponseEntity.ok(response);
    }
    
    /**
     * 发送设点命令
     */
    @PostMapping("/command/setpoint")
    public ResponseEntity<Map<String, Object>> sendSetpointCommand(
            @RequestParam String deviceId,
            @RequestParam int ioa,
            @RequestParam float value) {
        
        log.info("接收到设点命令请求: 设备={}, IOA={}, 值={}", deviceId, ioa, value);
        boolean success = iec104CommandService.sendSetpointCommand(deviceId, ioa, value);
        
        Map<String, Object> response = new HashMap<>();
        response.put("success", success);
        response.put("message", success ? "命令已发送" : "发送命令失败");
        
        return ResponseEntity.ok(response);
    }
    
    /**
     * 发送总召唤命令
     */
    @PostMapping("/command/interrogation")
    public ResponseEntity<Map<String, Object>> sendInterrogationCommand(
            @RequestParam String deviceId) {
        
        log.info("接收到总召唤命令请求: 设备={}", deviceId);
        boolean success = iec104CommandService.sendInterrogationCommand(deviceId);
        
        Map<String, Object> response = new HashMap<>();
        response.put("success", success);
        response.put("message", success ? "命令已发送" : "发送命令失败");
        
        return ResponseEntity.ok(response);
    }
    
    /**
     * 发送时钟同步命令
     */
    @PostMapping("/command/clock-sync")
    public ResponseEntity<Map<String, Object>> sendClockSyncCommand(
            @RequestParam String deviceId) {
        
        log.info("接收到时钟同步命令请求: 设备={}", deviceId);
        boolean success = iec104CommandService.sendClockSyncCommand(deviceId);
        
        Map<String, Object> response = new HashMap<>();
        response.put("success", success);
        response.put("message", success ? "命令已发送" : "发送命令失败");
        
        return ResponseEntity.ok(response);
    }
} 